const express = require('express');
const router = express.Router();
const utils = require("../utils/index.js");
const pools = require("../utils/pools.js");

// 获取车辆库存列表
router.post('/list', async (req, res) => {
    try {
        const { model, color } = req.body;
        let sql = 'SELECT * FROM vehicleinventory WHERE 1=1';

        // 构建搜索条件
        if (model) {
            sql += ' AND model LIKE ' + '"%'+model+'%"';
        }
        if (color) {
            sql += ' AND color LIKE ' + '"%'+color+'%"';
        }

        // 使用连接池执行查询
        await pools({
            sql,
            val: [],
            res,
            req,
            run: false
        });
    } catch (error) {
        console.error('获取车辆库存列表失败:', error);
        res.json(utils.error('获取车辆库存列表失败'));
    }
});

//获取车辆库存型号集合
router.post('/modelList', async (req, res) => {
    try {
        let sql = 'SELECT model FROM vehicleinventory';



        // 使用连接池执行查询
        await pools({
            sql,
            val: [],
            res,
            req,
            run: false
        });
    } catch (error) {
        console.error('获取车辆库存型号集合失败:', error);
        res.json(utils.error('获取车辆库存型号集合失败'));
    }
});

// 添加车辆库存
router.post('/add', async (req, res) => {
    try {
        const { model, color, configuration, stock_quantity, warning_threshold } = req.body;

        // 验证必填字段
        if (!model) {
            return res.json(utils.error('车型名称不能为空'));
        }

        const sql = `INSERT INTO vehicleinventory 
            (model, color, configuration, stock_quantity, warning_threshold) 
            VALUES (?, ?, ?, ?, ?)`;

        // 执行插入操作
        await pools({
            sql,
            val: [model, color, configuration, stock_quantity, warning_threshold],
            run: false,
            res,
            req
        });
    } catch (error) {
        console.error('添加车辆库存失败:', error);
        res.json(utils.error('添加车辆库存失败'));
    }
});

// 更新车辆库存信息
router.post('/update', async (req, res) => {
    try {
        const { id, model, color, configuration, stock_quantity, warning_threshold } = req.body;

        // 验证必填字段
        if (!id) {
            return res.json("车辆ID不能为空");
        }

        const sql = `UPDATE vehicleinventory 
            SET model=?, color=?, configuration=?, stock_quantity=?, warning_threshold=? 
            WHERE id=?`;

        // 执行更新操作
        await pools({
            sql,
            val: [model, color, configuration, stock_quantity, warning_threshold, id],
            run: false,
            res,
            req
        });
    } catch (error) {
        console.error('更新车辆库存信息失败:', error);
        res.json(utils.error('更新车辆库存信息失败'));
    }
});

// 删除车辆库存
router.post('/delete', async (req, res) => {
    try {
        const { id } = req.body;

        // 验证必填字段
        if (!id) {
            return res.json(utils.error('车辆ID不能为空'));
        }

        const sql = 'DELETE FROM vehicleinventory WHERE id = ?';

        // 执行删除操作
        await pools({
            sql,
            val: [id],
            run: false,
            res,
            req
        });
    } catch (error) {
        console.error('删除车辆库存失败:', error);
        res.json(utils.error('删除车辆库存失败'));
    }
});

module.exports = router;
